import React from "react";

class AppClass extends React.Component {
    state = {
        name: "传智播客",
        count: 0,
    };

    render() {
        return (
            <div>
                <p>{this.state.count}</p>
                <button>修改name</button>
                <button
                    onClick={() => {
                        // 1. this.setState({})
                        this.setState(
                            {
                                count: this.state.count + 1,
                            },
                            () => {
                                console.log(this.state.count);
                            }
                        );
                        // 2. this.setState(() => {return {}})
                        // 第一个参数是修改state的函数，第二个参数可以得到最新的state的值
                        this.setState(
                            (prevState) => {
                                return {
                                    // 推荐
                                    count: prevState.count + 1,
                                    // count: this.state.count + 1,
                                };
                            },
                            () => {
                                console.log(this.state.count);
                            }
                        );
                    }}>
                    count+1
                </button>
            </div>
        );
    }
}

export default AppClass;
